package com.example.service.impl;

import com.example.entity.FacilityMaintainer;
import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.entity.Staffs;
import com.example.mapper.FacilityMaintainerMapper;
import com.example.mapper.StaffsMapper;
import com.example.result.CommonResult;
import com.example.service.FacilityMaintainerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author: tobed
 * @date: 2022/11/15
 * @time: 10:01
 */
@Service
public class FacilityMaintainerServiceImpl implements FacilityMaintainerService {

    @Autowired
    FacilityMaintainerMapper facilityMaintainerMapper;

    @Autowired
    StaffsMapper staffsMapper;

    /*维修记录全查*/
    @Override
    public CommonResult list(FacilityMaintainer facilityMaintainer, PageRequest pageRequest) {
        MyPageHelper.startPage(pageRequest);
        List<FacilityMaintainer> list = facilityMaintainerMapper.list(facilityMaintainer);
        list.stream()
                .forEach(new Consumer<FacilityMaintainer>() {
                    @Override
                    public void accept(FacilityMaintainer facilityMaintainer) {
                        String[] split = facilityMaintainer.getMaintainerPeoples().split(",");
                        List<String> id = new ArrayList<>();
                        for (String s : split) {
                            id.add(s);
                        }
                        List<Staffs> listPeople = staffsMapper.listPeopleByIds(id);
                        listPeople.stream().forEach(staffs -> {
                            String names = "";
                            if (facilityMaintainer.getMaintainerPeoplesName() == null) {
                                names += staffs.getStaffName();
                            } else {
                                names += facilityMaintainer.getMaintainerPeoplesName() + "," + staffs.getStaffName();
                            }
                            facilityMaintainer.setMaintainerPeoplesName(names);
                        });
                    }
                });
        return CommonResult.ok(MyPageHelper.getPageResult(list));
    }

    /*删除维修记录*/
    @Override
    public CommonResult remove(Integer id) {
        int i = facilityMaintainerMapper.remove(id);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络延迟");
        }
    }

    /*添加设备维修记录*/
    @Override
    public CommonResult create(FacilityMaintainer facilityMaintainer) {
        facilityMaintainer.setCreateTime(new Date());
        int i = facilityMaintainerMapper.create(facilityMaintainer);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络延迟");
        }
    }

    /*审核*/
    @Override
    public CommonResult over(FacilityMaintainer facilityMaintainer) {
        facilityMaintainer.setUpdateTime(new Date());
        if (facilityMaintainer.getMaintainerStatus() == 1){
            facilityMaintainer.setMaintainerTime(new Date());
        }else{
            facilityMaintainer.setMaintainerTime(null);
        }
        int i = facilityMaintainerMapper.over(facilityMaintainer);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络延迟");
        }
    }
}
